<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('operation_logs', function (Blueprint $table) {
            $table->id();
            $table->timestamps();

            // 用户关联
            $table->unsignedBigInteger('user_id');
            $table->string('username'); // 记录操作时的用户名，避免关联查询
            $table->unsignedBigInteger('employee_id')->nullable(); // 关联员工

            // 操作信息
            $table->string('operation_type', 50); // 操作类型：create, update, delete, login, logout, etc.
            $table->string('operation_name', 100); // 操作名称
            $table->string('module', 50); // 所属模块
            $table->string('action', 50); // 具体动作

            // 目标对象
            $table->string('target_type', 50)->nullable(); // 目标对象类型：User, Employee, Department, etc.
            $table->string('target_id', 50)->nullable(); // 目标对象ID
            $table->string('target_name')->nullable(); // 目标对象名称

            // 操作详情
            $table->text('description')->nullable(); // 操作描述
            $table->json('old_data')->nullable(); // 操作前数据
            $table->json('new_data')->nullable(); // 操作后数据
            $table->json('change_data')->nullable(); // 变更数据
            $table->text('remark')->nullable(); // 备注信息

            // 请求信息
            $table->string('ip_address', 45); // IP地址
            $table->string('user_agent', 500)->nullable(); // 用户代理
            $table->string('session_id', 100)->nullable();

            // 结果信息
            $table->string('result')->default('success'); // success, failed, warning
            $table->text('error_message')->nullable(); // 错误信息

            // 业务信息
            $table->string('business_type', 50)->nullable(); // 业务类型
            $table->string('business_id', 50)->nullable(); // 业务ID

            // 审批相关
            $table->unsignedBigInteger('approval_id')->nullable(); // 关联审批流程
            $table->string('approval_status', 20)->nullable(); // 审批状态

            // 文件操作相关
            $table->unsignedBigInteger('file_id')->nullable(); // 关联文件
            $table->string('file_operation', 50)->nullable(); // 文件操作类型

            // 扩展字段
            $table->json('extra_data')->nullable(); // 扩展数据
            $table->string('level', 20)->default('info'); // 日志级别：debug, info, warning, error, critical

            // 索引
            $table->index(['user_id', 'created_at']);
            $table->index(['operation_type', 'created_at']);
            $table->index(['module', 'created_at']);
            $table->index(['target_type', 'target_id']);
            $table->index(['ip_address', 'created_at']);
            $table->index(['result', 'created_at']);
            $table->index(['business_type', 'business_id']);
            $table->index(['level', 'created_at']);
            $table->index(['created_at']);

            // 外键约束
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('employee_id')->references('id')->on('employees')->onDelete('set null');
            $table->foreign('file_id')->references('id')->on('files')->onDelete('set null');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('operation_logs');
    }
};
